/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.controler;

import com.camachosanchez.bean.Despinches;
import com.camachosanchez.bean.DetalleDespinche;
import com.camachosanchez.bean.InsumosDespinche;
import com.camachosanchez.controler.exceptions.NonexistentEntityException;
import com.camachosanchez.util.JPAUtil;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

/**
 *
 * @author ZULLY
 */
public class DetalleDespincheJpaController implements Serializable {

    public DetalleDespincheJpaController(EntityManagerFactory emf) {
        this.emf = JPAUtil.getEntityManagerFactory();
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(DetalleDespinche detalleDespinche) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InsumosDespinche idInsumosDespinche = detalleDespinche.getIdInsumosDespinche();
            if (idInsumosDespinche != null) {
                idInsumosDespinche = em.getReference(idInsumosDespinche.getClass(), idInsumosDespinche.getId());
                detalleDespinche.setIdInsumosDespinche(idInsumosDespinche);
            }
            Despinches idDespinches = detalleDespinche.getIdDespinches();
            if (idDespinches != null) {
                idDespinches = em.getReference(idDespinches.getClass(), idDespinches.getId());
                detalleDespinche.setIdDespinches(idDespinches);
            }
            em.persist(detalleDespinche);
            if (idInsumosDespinche != null) {
                idInsumosDespinche.getDetalleDespincheList().add(detalleDespinche);
                idInsumosDespinche = em.merge(idInsumosDespinche);
            }
            if (idDespinches != null) {
                idDespinches.getDetalleDespincheList().add(detalleDespinche);
                idDespinches = em.merge(idDespinches);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(DetalleDespinche detalleDespinche) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            DetalleDespinche persistentDetalleDespinche = em.find(DetalleDespinche.class, detalleDespinche.getId());
            InsumosDespinche idInsumosDespincheOld = persistentDetalleDespinche.getIdInsumosDespinche();
            InsumosDespinche idInsumosDespincheNew = detalleDespinche.getIdInsumosDespinche();
            Despinches idDespinchesOld = persistentDetalleDespinche.getIdDespinches();
            Despinches idDespinchesNew = detalleDespinche.getIdDespinches();
            if (idInsumosDespincheNew != null) {
                idInsumosDespincheNew = em.getReference(idInsumosDespincheNew.getClass(), idInsumosDespincheNew.getId());
                detalleDespinche.setIdInsumosDespinche(idInsumosDespincheNew);
            }
            if (idDespinchesNew != null) {
                idDespinchesNew = em.getReference(idDespinchesNew.getClass(), idDespinchesNew.getId());
                detalleDespinche.setIdDespinches(idDespinchesNew);
            }
            detalleDespinche = em.merge(detalleDespinche);
            if (idInsumosDespincheOld != null && !idInsumosDespincheOld.equals(idInsumosDespincheNew)) {
                idInsumosDespincheOld.getDetalleDespincheList().remove(detalleDespinche);
                idInsumosDespincheOld = em.merge(idInsumosDespincheOld);
            }
            if (idInsumosDespincheNew != null && !idInsumosDespincheNew.equals(idInsumosDespincheOld)) {
                idInsumosDespincheNew.getDetalleDespincheList().add(detalleDespinche);
                idInsumosDespincheNew = em.merge(idInsumosDespincheNew);
            }
            if (idDespinchesOld != null && !idDespinchesOld.equals(idDespinchesNew)) {
                idDespinchesOld.getDetalleDespincheList().remove(detalleDespinche);
                idDespinchesOld = em.merge(idDespinchesOld);
            }
            if (idDespinchesNew != null && !idDespinchesNew.equals(idDespinchesOld)) {
                idDespinchesNew.getDetalleDespincheList().add(detalleDespinche);
                idDespinchesNew = em.merge(idDespinchesNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = detalleDespinche.getId();
                if (findDetalleDespinche(id) == null) {
                    throw new NonexistentEntityException("The detalleDespinche with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            DetalleDespinche detalleDespinche;
            try {
                detalleDespinche = em.getReference(DetalleDespinche.class, id);
                detalleDespinche.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The detalleDespinche with id " + id + " no longer exists.", enfe);
            }
            InsumosDespinche idInsumosDespinche = detalleDespinche.getIdInsumosDespinche();
            if (idInsumosDespinche != null) {
                idInsumosDespinche.getDetalleDespincheList().remove(detalleDespinche);
                idInsumosDespinche = em.merge(idInsumosDespinche);
            }
            Despinches idDespinches = detalleDespinche.getIdDespinches();
            if (idDespinches != null) {
                idDespinches.getDetalleDespincheList().remove(detalleDespinche);
                idDespinches = em.merge(idDespinches);
            }
            em.remove(detalleDespinche);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<DetalleDespinche> findDetalleDespincheEntities() {
        return findDetalleDespincheEntities(true, -1, -1);
    }

    public List<DetalleDespinche> findDetalleDespincheEntities(int maxResults, int firstResult) {
        return findDetalleDespincheEntities(false, maxResults, firstResult);
    }

    private List<DetalleDespinche> findDetalleDespincheEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from DetalleDespinche as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public DetalleDespinche findDetalleDespinche(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(DetalleDespinche.class, id);
        } finally {
            em.close();
        }
    }

    public int getDetalleDespincheCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from DetalleDespinche as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
